package Eyjafjalla.action;

import Eyjafjalla.patch.CloudPatch;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.common.DrawCardAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;

import java.util.ArrayList;

public class SwapHandCloudAction extends AbstractGameAction {

    public SwapHandCloudAction(boolean same){
        this.duration = Settings.ACTION_DUR_FAST;
        startDuration = duration;
        actionType = ActionType.CARD_MANIPULATION;
        isFire = same;
    }

    boolean isFire;

    ArrayList<AbstractCard> h;
    ArrayList<AbstractCard> c;

    @Override
    public void update() {
        if(duration == startDuration){
            if(AbstractDungeon.player.hand.size()>5){
                this.isDone = true;
                return;
            }
            h = new ArrayList<>(AbstractDungeon.player.hand.group);
            c = new ArrayList<>(CloudPatch.cloudGroup.group);
            if(isFire){
                int max = Math.max(h.size(),c.size());
                if(h.size()<max){
                    addToTop(new DrawCloudAction(max-h.size()));
                }
                if(c.size()<max){
                    addToTop(new DrawCardAction(max-c.size()));
                }
            }
        }
        else {
            if(h.size()>0){
                CloudPatch.moveToCloud(AbstractDungeon.player.hand,h.get(0));
                h.remove(0);
            }
            if(c.size()>0){
                CloudPatch.resetBeforeRemove(c.get(0));
                CloudPatch.cloudGroup.moveToHand(c.get(0));
                c.remove(0);
            }
        }


        this.tickDuration();
    }
}
